package com.hurt.competition.player.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hurt.competition.common.contans.CoreContants;
import com.hurt.competition.common.exception.CoreException;
import com.hurt.competition.common.exception.LoginException;
import com.hurt.competition.common.vo.PageResult;
import com.hurt.competition.player.dto.SysUserReq;
import com.hurt.competition.player.mapper.SysUserMapper;
import com.hurt.competition.player.model.CompetitionPlayer;
import com.hurt.competition.player.model.SysUser;
import com.hurt.competition.player.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Override
    public PageResult<SysUser> queryList(SysUserReq req) {
        // 开启分页，只针对遇到的第一个对数据库的查询分页
        Page<SysUser> page = PageHelper.startPage(req.getPageNumber(), req.getPageSize());

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        // 模糊查询，like方法的第一个参数为条件，如果为true，则进行后续的操作
        queryWrapper
                .eq(StringUtils.isNotBlank(req.getType()), SysUser::getType, req.getType())
                .like(StringUtils.isNotBlank(req.getRealName()),SysUser::getRealName,req.getRealName())
                .like(StringUtils.isNotBlank(req.getUserName()),SysUser::getUserName,req.getUserName());

        list(queryWrapper);
        // 直接返回page，因为数据在page里封装好了

        PageResult pageResult = new PageResult();
        pageResult.setList(page.getResult());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Override
    public SysUser insertOrUpdate(SysUser user) {
        if (existName(user)) {
            throw new CoreException("用户名重复");
        }

        this.saveOrUpdate(user);
        return user;
    }

    @Override
    public SysUser getUserByLogin(SysUser user) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserName, user.getUserName()).eq(SysUser::getUserPwd, user.getUserPwd());
        SysUser loginUser = getOne(queryWrapper, false);

        // 如果没查询到 则返回用户名或密码错误
        if (loginUser == null) {
            throw new LoginException(CoreContants.PASSWORD_ERROR);
        }
        return loginUser;
    }

    @Override
    public SysUser queryInfo(SysUser user) {
        return this.getById(user.getId());
    }

    public boolean existName(SysUser user) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        if (user.getId() != null) {
            queryWrapper.ne(SysUser::getId, user.getId());
        }
        queryWrapper.eq(SysUser::getUserName, user.getUserName());
        return getOne(queryWrapper, false) != null;
    }
}
